\section{Implementation}
Two applications had to be implemented: The analysis software
(Movyzer.application) is used to evaluate und process the data, logged
by accelerometers and electromyograms. The logging software (Movyzer.mobile)
enables Android mobile phones to log movements by using the build-in
accelerometer.

\subsection{Movyzer.application}
The Movyzer analysis application is a tool to evaluate and process GT3X and PSG
data. The main task is to visualize the data simultaneously and to extract leg
movements (LM) and periodic leg movements (PLM). Furthermore the GT3X data can
be compared to the PSG data and the similarity can be calculated. Thus the
accuracy of the accelerometers and the quality of its data can be determined. In
addition it can be decided, whether the accelerometers are qualified to be used
in the PLMD diagnosis for the detection of periodic leg movements.

Since the PLM Disorder usually occurs on one leg only, both legs have to be
monitored and the data of both legs has to be evaluated and compared to the PSG
data.
\subsubsection{GUI}
\begin{figure}[htp] 
\begin{center}
  \includegraphics[width=1\textwidth]{imgs/screenshot_app_1.jpg}
  \caption[Movyzer.application: Main Window]{Main window of the Movyzer
  application: (1) GT3X Graph (left leg), (2) GT3X Graph (right leg), (3) PSG
  Graph, (4) Log field, (5) Parameters}
  \label{fig:screenshot_app}
\end{center}
\end{figure}
In figure \ref{fig:screenshot_app} the main window of the Movyzer application
is shown. The main window contains 3 graphs and one log field, which shows some
useful and additional information.

\paragraph{Parameters:}
For each one of the three graphs, several parameters can be set:
\begin{itemize}
  \item Earth gravity: This parameter corresponds to the earth gravity in
  ${mm}/s^2$. This parameter is only required for RAW-data and data from
  mobile phones.
  \item Threshold (gravity): All measured events, which intensity is smaller
  than this parameter (in ${mm}/s^2$), aren't considered to be movements. This
  parameter is only required for RAW-data and data from mobile phones.
  \item Threshold (LM): All movements, which intensity is smaller than this
  parameter, aren't considered to be LMs.
  \item Threshold (TM): If the time frame between two movements is smaller than
  this parameter (in milliseconds), they are considered to be one single
  movement. The maximal value of this parameter is: 1000.
\end{itemize}

\paragraph{Graphs:}
The upper two graphs are used to visualize the GT3X data for the left (graph 1)
and for the right (graph 2) leg. The third graph is used to visualize the PSG
data. Figure \ref{fig:screenshot_graph} shows a screenshot of a GT3X graph. All
the three graphs have the same appearance: the red line shows the intensity of
the movement, the blue line shows the PLMs. The LMs are visualized as red,
transparent bars.

\begin{figure}[htp]
\begin{center} 
  \includegraphics[width=1\textwidth]{imgs/screenshot_graph.JPG}
  \caption[GT3X graph]{GT3X graph: the red line shows intensity, the red
  transparent bars show the LMs, the blue line borders the PLMs}
  \label{fig:screenshot_graph}
\end{center}
\end{figure}

There can be zoomed in by clicking to the start position in one
of the three graphs, and dragging the mouse to the right. By zooming into one
graph, all the three graphs are zooming in simultaneously. By dragging the mouse
to the left, the zoom is reset to the original magnification.

The context menu can be shown by right-clicking on one of the graphs. There are
several useful functionalities: print the graph, export the graph as
a PNG-Image, zoom in/out, adjust the graph and change colors and other
settings.

\paragraph{Log field:}
The log field shows advanced information about the current data and about the
calculated measurements.
  
Different information is displayed for the different data files.
When a GT3X da\-ta file is loaded, the following information is shown:
\newline
\fbox{\parbox{1\textwidth}{
\texttt{Loading GT3X-File: C:$\backslash$GT3X-XXXXXX.csv
\newline
Serial Number: MAT2A290XXXXX
\newline
Start Time: \ \ \ Tue Apr 06 23:00:00 CEST 2010
\newline
Download Time: Wed Apr 07 16:00:52 CEST 2010
\newline
Epoch Period: \ 00:00:01
\newline
End Time: \ \ \ \ \ Wed Apr 07 16:00:33 CEST 2010
\newline
GT3X-File loaded, 61233 entries found
\newline
Analyzing (Left): 104 LM found
\newline
Analyzing (Left): 4 PLM found
}}}
\newline
The first entry is the type and the path of the loaded file. The next entries
correspond to the header information. The header contains several data about
the settings of the sensor. This includes the serial number, the start, the end
time and the epoch period. Next to the header information, the result of the
analysis is given: The number of logged events, the number of leg movements (LM)
and of the periodic leg movements (PLM).

When opening PSG files, less information is displayed because those files
don't con\-tain a header. The log entry could look like the following:
\newline
\fbox{\parbox{1\textwidth}{
\texttt{Loading PSG-File: C:$\backslash$PSG-XXXXXXX.csv
\newline
Start Time: Tue Apr 06 22:52:36 CEST 2010
\newline
End Time: \ \ Wed Apr 07 06:34:39 CEST 2010
\newline
PSG-File loaded, 105 entries found
\newline
Analyzing (PSG): 103 LM found
\newline
Analyzing (PSG): 4 PLM found
}}}
\newline
When both, a GT3X and a PSG file, are loaded, the calculated similarity
between those two files is shown in the log field. The similarity is
calculated twice: once for the LMs and once for the PLMs. Such a log entry could
look like the following (for the left leg):
\newline
\fbox{\parbox{1\textwidth}{
\texttt{Left Similarity (LM): 10.695652173913043\%
\newline
Left Similarity (PLM): 0.0\%
}}}


\paragraph{Similarity measurement}
\begin{figure}[htp]
\begin{center}
  \includegraphics[width=0.4\textwidth]{imgs/screenshot_simm.JPG}
  \caption[Similarity measurement]{Similarity measurement}
  \label{fig:similarity_measurement}
\end{center}
\end{figure}
Since it can happen, that the GT3X files or the PSG file are shifted by some
seconds, the application includes a tool readjust this shift. This tool
calculates and visualizes the similarity of the selected GT3X graph (left or
right) with the PSG graph, for a given timeframe. The tool for the similarity adjustment is
shown in figure \ref{fig:similarity_measurement}. It requires the following
parameters:
\begin{itemize}
  \item start time: the maximal time shift backwards in milliseconds
  \item end time: the maximal time shift forwards in milliseconds
  \item step size: the interval in milliseconds for which the similarity is
  calculated. This parameter corresponds to the smoothness of
  the resulting graph.
\end{itemize}
The time shift for the best similarity is automatically inserted into
the ``Best time shift'' field. By clicking on the ``Set time shift'' button,
the GT3X graph will be shifted by the given timeframe. Of course it is also
possible to apply a different time shift.

\subsubsection{Implementation Details}
For the implementation of the analysis software, different structures have
been necessary:
\begin{itemize}
  \item \code{MovementSeries}: contains the list of movements and the
  functions to detect LMs and PLMs
  \item \code{ExtCalendar}: extends the Calendar class, contains some useful
  functions for date calculations
  \item \code{MovyzerPlot}: based on JFreeChart, displays the three graphs
  \item \code{GT3XData}: represents the GT3X data, contains the base I/O
  functions
  \item \code{PSGData}: represents the PSG data, contains the base I/O functions
\end{itemize}

\paragraph{MovementSeries:} This class represents the LM and PLM movements from
a GT3X or PSG file. Therefore it contains a list of \code{Movement} elements.
One single movement consists of these values: \code{startTime}, \code{endTime}
and \code{intensity}. The following functions are the most important functions
the \code{MovementSeries} contains:
\begin{itemize}
  \item \code{MovementSeries getLMSeries()}: returns a new MovementSeries
  variable containing the leg movements. The rules for the extraction of the LMs
  can be found in paragraph \ref{sec:detection}.
  \item \code{MovementSeries getPLMSeries()}: returns a new MovementSeries
  containing the periodic leg movements. Again the rules can be found in
  paragraph \ref{sec:detection}.
  \item \code{double getSimilarity(MovementSeries series)}: returns a percental 
  value corresponding to the similarity with the given MovementSeries
  \code{series}. Therefore the overlapping time (the intersection of the two
  movement sets) of all movements in both MovementSeries is divided by the union
  of the two movement sets. The similarity $S_{M_1,M_2}$ for two movements sets
  (MovementSeries) $M_1$ and $M_2$ is calculated thusly:
  \newline
  \begin{equation}
  	S_{M_1,M_2}=\frac{M_1 \bigcap M_2}{M_1 \bigcup M_2}
  \end{equation}
  \item \code{void setShift(int milliseconds)}: is used to shift all the
  movements in this MovementSeries by the given time in milliseconds.
\end{itemize}

\paragraph{ExtCalendar} This class extends the \code{java.util.Calendar} class
and is used for the date and time representation, because most functions in 
\code{java.util.Date} have been deprecated. The new functions in
\code{ExtCalendar} are required for the calculations and measurements in this
application. Those added functions include:
\begin{itemize}
  \item \code{static ExtCalendar timeDiff(ExtCalendar cal1,ExtCalendar cal2)}:
  \newline
  This static function subtracts the time \code{cal2} from the time 
  \code{cal1}. Only the time is subtracted, not the date. This
  function is need to calculate the duration of a LM or a PLM.
  \item \code{public RegularTimePeriod getRegularTimePeriod()}: 
  converts the ExtCalendar data to the RegularTimePeriod format. The
  RegularTimePeriod format is required by JFreeChart in order to visualize time
  charts.
  \item \code{public RegularTimePeriod getPrevRegularTimePeriod()}: 
  returns the nearest time before the current time. In this implementation the
  function subtracts 1 millisecond from the current time.
  \item \code{public RegularTimePeriod getNextRegularTimePeriod()}: Similar to
  the upper function, it adds 1 millisecond to the current time.
\end{itemize}

\paragraph{MovyzerPlot} This class is based on the CombinedDomainXYPlot from the
free Java chart library: JFreeChart. The CombinedDomainXYPlot allows several
charts with multiple domains to be visualized simultaneously. 

The MovyzerPlot class includes three functions to set the data for the three
graphs (left GT3X, right GT3X and PSG). Here the function
for visualizing the movements and the PLMs for the left GT3X file:
\newline
\code{void setGT3XDataLeft(TimeSeries mSeries,TimeSeries
plmSeries);}

\paragraph{GT3XData} This class includes the basic I/O functionalities for the
GT3X data. When loading a GT3X file, all movements are stored into a list and
can be converted into different data formats.
The most important functions are:
\begin{itemize}
  \item \code{static int preview(String path) throws Exception}: analyses
  the file and returns the number of coherent movements. Thus the user can
  get a first impression about the file, when selecting it in the file browser.
  If the selected file is not a GT3X file, an Exception is thrown.
  \item \code{boolean loadFile(String path}: loads the given file, stores all
  entries into a list and returns whether the loading was successful or not.
  \item \code{MovementSeries getMovementSeries()}: returns all the movements in
  the MovementSeries format. Thereby the data can be evaluated in regards of LMs
  and PLMs.
  \item \code{TimeSeries getTimeSeries()}: returns all the movements in the
  TimeSeries format. This format is required by JFreeGraph in order to visualize
  the movements.  
  \item \code{double getMovementIntensity(Integer[] data)}: returns the
  intensity of the given data set. The intensity $i$ corresponds to the 
  length of the acceleration vector $\vec{accel}$.
  \begin{equation}
  	i=\left| \vec{accel} \right|
  \end{equation}
  If the current data was logged by the Movyzer.mobile application or the data
  was logged in RAW mode, the earth gravity $g$ has to be subtracted:
  \begin{equation}
  	i=\left| \left| \vec{accel} \right| - g \right|
  \end{equation}
  The earth gravity is set by default to $g=9.81$ but it can be changed with
  the ``Earth gravity'' parameter.
\end{itemize}

\paragraph{PSGData} This class is the equivalent to the GT3XData class, but for
PSG data. The same functions are available: \code{loadFile, preview, getTimeSeries}
etc.

\subsubsection{LM \& PLM detection}
\label{sec:detection}
The detection of the LMs and the PLMs is based on a set of rules which are used
to detect PLMs in EMG data. By applying those rules on data from accelerometers,
not all rules can be considered. The following rules are used to detect LMs:
\begin{itemize}
  \item The minimal duration of a LM is 0.5 seconds
  \item The maximal duration of a LM is 10 seconds
  \item The intensity is bigger than the ``Threshold (LM)'' parameter.
\end{itemize}
The following rules are used to detect PLMs:
\begin{itemize}
  \item A PLM consists of at least 4 consecutive LMs
  \item The minimal time that can be between two consecutive LMs belonging to a
  PLM is 5 seconds
  \item The maximal time that can be between two consecutive LMs belonging to a
  PLM is 90 seconds
\end{itemize} 

\subsection{Movyzer.mobile}
Movyzer.mobile is a mobile application based on Android (Version 1.6) and can be 
used on mobile phones to log movements. The application was implemented
considering its simplicity and usability. Therefore it is only equipped with the
following basic functions:
\begin{itemize}
  \item Start logging
  \item Show statistics
  \item Stop logging
\end{itemize}
When the logging has been stopped the data is saved to the ``/download''
folder on the memory card.

\subsubsection{GUI}
\begin{figure}[htp]
\begin{center}
  \includegraphics[width=0.397\textwidth]{imgs/screen_mobile_1.JPG}
  \includegraphics[width=0.4\textwidth]{imgs/screen_mobile_11.JPG}
  \caption[Movyzer.mobile main menu and popup window]{Movyzer.mobile main menu
  (left) and popup window (right)}
  \label{fig:mobile_mainmenu}
\end{center}
\end{figure}

There are only two options in the main menu: Start the logging and close the
application. In figure \ref{fig:mobile_mainmenu} the left image a
screenshot of the main menu can be seen. The right image shows the popup window which asks
for the logging interval, once the logging of the movements has been started.
\begin{figure}[htp]
\begin{center}
  \includegraphics[width=0.4\textwidth]{imgs/screen_mobile_2.JPG}
  \includegraphics[width=0.395\textwidth]{imgs/screen_mobile_3.JPG}
  \caption[Movyzer.mobile logging menu and statistics]{Movyzer.mobile logging
  menu (left) and statistics (right)}
  \label{fig:mobile_logandstats}
\end{center}
\end{figure}

A screenshot of the application during the
logging process can be seen in figure \ref{fig:mobile_logandstats}. Next to
it is a screenshot of the statistics window, which visualizes the most
important information: the number of logged events, the logging interval and the
starting time.

\subsubsection{Implementation Details}
The mobile application has two ``Activities'', corresponding to the two views:
MainView and StatsView. The only purpose of those two Activities is to provide
the user with a GUI. In the MainView the logging process can be started and
stopped. The logging process itself is implemented in the class
\code{AccelService}. This class extends the Android \code{Service} class, which
allows the process to be run in the background. \code{AccelService} also
implements the class \code{SensorEventListener}, which allows the process to
read out the accelerometer. 
The main function of the class \code{AccelService} are:
\begin{itemize}
  \item \code{void startTimer(int rate)}: sets the timer interval to the given
  rate and starts the timer. The timer itself calls the function
  \code{addSensorData()} periodically.
  \item \code{void addSensorData()}: logs the latest accelerometer data. The
  acceleration data includes the earth gravity, so this constant has to be
  removed by the analysis process. The unit of the logged data is $m/{s^2}$.
  \item \code{void onSensorChanged(SensorEvent event)}: This function is called
  whenever the accelerometer provides new data. This function is not called
  periodically but as fast as possible. When the phone is idle, new data is
  provided in short intervals, otherwise the intervals increase. Thus a timer
  is necessary to log the data.
  \item \code{int saveMovements()}: saves the logged data as a GT3X file into
  the ``/download'' folder on the memory card.
\end{itemize}

